#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <vector>
using namespace std;
#define MAX_SIFNUM 31
static vector<int> sigarr = {2};
static void handler(int signo)
{
    cout << signo << " 号信号已经被递达！！" << endl;
}
static void show_pending(const sigset_t &pending)
{
    for (int signo = MAX_SIFNUM; signo >= 1; signo--)
    {
        if (sigismember(&pending, signo))
            cout << "1";
        else
            cout << "0";
    }
    cout << endl;
}
void test()
{
    for (const auto &sig : sigarr)
    {
        signal(sig, handler);
    }
    sigset_t block, oblock, pending;
    sigemptyset(&block);
    sigemptyset(&oblock);
    sigemptyset(&pending);
    for (const auto &sig : sigarr)
    {
        sigaddset(&block, sig);
    }
    sigprocmask(SIG_SETMASK, &block, &oblock);
    int cnt = 10;
    while (true)
    {
        sigemptyset(&pending);
        sigpending(&pending);
        show_pending(pending);
        sleep(1);
        if(cnt-- == 0)
        {
            cout<<"恢复对信号的屏蔽, 不屏蔽任何信号"<<endl;
            sigprocmask(SIG_SETMASK,&oblock,&block);
        }

    }
}
int main()
{
    test();
    return 0;
}